###  Figure A4  ###

library(ggplot2)
library(tidyr)
library(dplyr)

## Have to run "Figure_A4_st_prep.do" first to create "estimation_results_A4.csv" ##

# Load data with estimation results
file_path <- "~/Dropbox/Replication_MVC/Datasets/datasets_analysis/estimation_results_A4.csv"  

estimation_results <- read.csv(file_path)

# Remove the "=" character from all the observations in the data frame
estimation_results_cleaned <- data.frame(lapply(estimation_results, function(x) gsub("=", "", x)))

# Remove rows where "Months" variable is empty
estimation_results_cleaned <- subset(estimation_results_cleaned, X. != "")

# Separate the last column into two different columns
estimation_results_cleaned <- separate(estimation_results_cleaned, col = X..1, into = c("low", "up"), sep = ",")

colnames(estimation_results_cleaned) <- c("Months", "coeff", "low", "up")

# Transform the columns to numeric
estimation_results_cleaned$coeff <- as.numeric(as.character(estimation_results_cleaned$coeff))
estimation_results_cleaned$low <- as.numeric(as.character(estimation_results_cleaned$low))
estimation_results_cleaned$up <- as.numeric(as.character(estimation_results_cleaned$up))
estimation_results_cleaned <- estimation_results_cleaned %>%
    mutate(months = as.numeric(gsub("[^0-9.]", "", Months)))

plotdat <- estimation_results_cleaned

# Figure A4" Peer Effects by months in armed group

gA4 <- ggplot(plotdat, aes(x = months, y = coeff)) +
    geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) +
    geom_errorbar(aes(ymin = low, ymax = up),
                  lwd = 1, position = position_dodge(width = 0.5),
                  width = 0.25) +
    geom_point(position = position_dodge(width = 0.5)) +
    theme_minimal() +
    theme(text = element_text(size = 18)) +
    scale_x_continuous(name = "Months in Armed Group") +
    scale_y_continuous(name = "Peer Effect", limits = c(-0.5, 2))
